package com.lzxuni.modules.dataAcquisition;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzxuni.common.annotation.SysLog;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.entity.Tree;
import com.lzxuni.modules.dataAcquisition.entity.Chain;
import com.lzxuni.modules.dataAcquisition.entity.ChainLink;
import com.lzxuni.modules.dataAcquisition.service.ChainLinkService;
import com.lzxuni.modules.dataAcquisition.service.ChainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 追溯链条-环节 前端控制器
 * </p>
 *
 * @author wzx
 * @since 2019-06-25
 */
@RestController
@RequestMapping("/LR_DataAcquisition/ChainLink")
public class ChainLinkController extends BaseController {

    @Autowired
    private ChainService chainService;

    @Autowired
    private ChainLinkService chainLinkService;

    @RequestMapping("/Index")
    @SysLog(categoryId = 2, module = "追溯链条-环节", operateType = "访问")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("/LR_DataAcquisition/ChainLink/index");
        return mv;
    }

    @RequestMapping("/GetList")
    public Object GetList(String pagination, ChainLink chainLink, String Keyword) throws SQLException {
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        chainLink.setLinkName(Keyword);
        //PageData pageData = getPageData(chainLinkService.queryPage(pageParameter, chainLink));
        List<Map<String, Object>> mapList = chainLinkService.queryPage(chainLink);
        return R.ok().put("data", mapList);
    }

    @RequestMapping("/Form")
    public ModelAndView insert(String parentId, String id) throws Exception {
        ModelAndView mv = new ModelAndView("/LR_DataAcquisition/ChainLink/form");
        ChainLink chainLink = new ChainLink();
        if (StringUtils.isNotEmpty(id)) {
            chainLink = chainLinkService.getById(id);
            mv.addObject("parentId", chainLink.getParentId());
        }else{
            mv.addObject("parentId", parentId);
            Integer i = chainLinkService.count(new QueryWrapper<ChainLink>().eq("parent_id", parentId));
            if (i == 0) {
                String parentCode = chainService.getById(parentId).getChainCode();
                chainLink.setLinkCode(parentCode + "01");
            }else{
                List<ChainLink> chainLinks = chainLinkService.list(new QueryWrapper<ChainLink>().eq("parent_id", parentId).orderByDesc("create_time"));
                int c = Integer.parseInt(chainLinks.get(0).getLinkCode());
                c++;
                chainLink.setLinkCode(String.valueOf(c));
            }
        }
        mv.addObject("chainLink", chainLink);
        return mv;
    }

    @RequestMapping("/SaveForm")
    @SysLog(categoryId = 3, module = "追溯链条-环节", operateType = "增/改")
    public Object insertDo(HttpServletRequest request, String keyValue, ChainLink chainLink) throws Exception {
        chainLink.setParentId(chainLink.getChainId());
        if (StringUtils.isEmpty(keyValue)) {
            chainLink.setCreateTime(new Date());
            chainLinkService.save(chainLink);
            return R.ok("保存成功");
        } else {
            chainLink.setId(keyValue);
            chainLinkService.updateById(chainLink);
            return R.ok("修改成功");
        }
    }

    @RequestMapping("/DeleteForm")
    @SysLog(categoryId = 3, module = "追溯链条-环节", operateType = "删除")
    public Object delete(String keyValue) {
        chainLinkService.removeById(keyValue);
        return R.ok("删除成功");
    }

    @RequestMapping("/GetSelectTree")
    public Object GetSelectTree(String nature)  {
        List<Tree> trees = chainLinkService.getTreeSelectTree(nature);
        return R.ok().put("data",trees);
    }

}

